var queryUrl = "/user/query";


//全局定义一次, 加载formSelects
layui.config({
    base: '/' //此处路径请自行处理, 可以使用绝对路径
}).extend({
    xmSelect: 'xm-select'
});

//加载表格
layui.use(['table', 'form', 'xmSelect'], function () {
    var table = layui.table;
    var formSelects = layui.formSelects;

    var tableInit=table.render({
        elem: '#table'
        ,toolbar: '#toolbarDemo'
        , url: "/user/query"
        , page: true
        // ,parseData: function(res){ //res 即为原始返回的数据
        //     console.log(res)
        //     return {
        //         "data": res.data //解析数据列表
        //         ,"code":res.code
        //         ,"count":res.count
        //     };
        // }
        ,id:'id'
        , cols: [[
            {field: 'id', title: 'ID', width: 80, fixed: 'left', type: 'checkbox'}
            , {field: 'username', title: '账号', width: 177}
            , {field: 'email', title: '邮箱', width: 177}
            , {
                field: 'state', title: '账号状态', width: 120, templet: function (d) {
                    if (d.state == 2)
                        return "锁定";
                    else
                        return "生效";
                }
            }
            , {field: 'name', title: '名字', width: 177}
            , {field: 'password', title: '密码', width: 177}
        ]]
    });


    var form = layui.form;
    //新增或更新
    submitForm('add_update', '/user/add');

    //初始化selector
    initSelectorData('roleStr', '#roleList', '/role/findByRoleName');


    //监听事件
    table.on('toolbar(userTable)', function(obj){
        var selector = table.checkStatus('id');
        switch(obj.event){
            case 'add':
                layer.msg('请在右方添加！');
                break;
            case 'delete':
                console.log(JSON.stringify(selector.data));
                doPost('/user/lockUser', {'usersJson' : JSON.stringify(selector.data)}, tableInit);
                break;
            case 'update':
                layer.msg('编辑');
                break;
        };
    });

});

